﻿using System.Data;
using MySqlConnector;

namespace From._1124;

public partial class AddBookFrom : Form
{
    public AddBookFrom()
    {
        InitializeComponent();
    }

    private void AddBookFrom_Load(object sender, EventArgs e)
    {
        Init();
    }

    private void Init()
    {
        //加载出版社信息
        GetAllPublishers();
        //加载分类信息
        //加载书架信息
    }

    private void GetAllPublishers()
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        string sql = "SELECT publisher_id,publisher_name  FROM publisher_info";
        //创建适配器对象
        MySqlDataAdapter adapter=new MySqlDataAdapter(sql ,connection);
        //创建数据集
        DataSet dataSet = new DataSet();
        //填充数据
        adapter.Fill(dataSet, "publisher_info");
        //绑定数据
        DataTable? dataTable=dataSet.Tables["publisher_info"];
        //插入全部行
        DataRow row=dataTable.NewRow();
        row[0] = '0';
        row[1] = "全部";
        dataTable.Rows.InsertAt(row,0);
        //绑定数据
        this.publishId.DataSource=dataTable;
        this.publishId.ValueMember = dataTable.Column[0].ColumnName;
    }

    private void label2_Click(object sender, EventArgs e)
    {
        throw new System.NotImplementedException();
    }

    private void label3_Click(object sender, EventArgs e)
    {
        throw new System.NotImplementedException();
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        throw new System.NotImplementedException();
    }

    private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {
        throw new System.NotImplementedException();
    }

    private void textBox4_TextChanged(object sender, EventArgs e)
    {
        throw new System.NotImplementedException();
    }

    private void label12_Click(object sender, EventArgs e)
    {
        throw new System.NotImplementedException();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;");
        connection.Open();
        string sql = "insert  into `book_info`(`isbn`,`book_name`,`author`,`publisher_id`,`publish_date`,`book_category`,`book_price`,`book_pages`,`book_desc`,`book_status`,`shelf_id`,`purchase_date`,`book_version`,`create_time`) " +
                     "values (@isbn,@book_name,'刘增杰',@publisher_id,'2021-06-01','计算机科学',59.80,320,'讲解MySQL基础与实战开发',1,1,'2023-01-10','第2版','2025-10-10 10:11:29'),";
        //创建command 对象
        using var command=new MySqlCommand(sql ,connection);
        //确定参数
        command.Parameters.AddWithValue("isbn",this.isbn.Text);
        command.Parameters.AddWithValue("bookName",this.bookName.Text);
        command.Parameters.AddWithValue("author",this.author.Text);
        command.Parameters.AddWithValue("publisher_id",this.publisher_id);
        //执行新增
        int count = CommandType.ExecuteNonQuery();
        if (count > 0)
        {
            MessageBox.Show("新增成功");
            this.Dispose();
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        AddBookFrom addBookFrom = new AddBookFrom();
        DialogResult result=addBookFrom.ShowDialog();
        if (result == DialogResult.OK)
        {
            GetAllPublishers();
        }
    }
}